/**
 * 
 */
package com.ssyt.ssh.dao;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.SessionFactory;
import org.hibernate.query.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;

import com.ssyt.ssh.entity.Department;
import com.ssyt.ssh.entity.Employee;

/**
 * 操作员工功能方法实现
 * @author Administrator
 *
 */
@Repository("empDao")
@SuppressWarnings("all")
public class EmpDAOImpl implements EmpDAO {

	@Autowired
	@Qualifier("mySessionFactory")
	private SessionFactory sessionFactory;
	
	public void addEmp(Employee emp) {
		sessionFactory.getCurrentSession().persist(emp);

	}

	/* (non-Javadoc)
	 * @see com.ssyt.ssh.dao.EmpDAO#deleteEmp(com.ssyt.ssh.entity.Employee)
	 */
	@Override
	public void deleteEmp(Employee emp) throws Throwable{
		sessionFactory.getCurrentSession().delete(emp);

	}

	/* (non-Javadoc)
	 * @see com.ssyt.ssh.dao.EmpDAO#getEmp(com.ssyt.ssh.entity.Employee)
	 */
	@Override
	public Employee getEmp(Employee emp) {
		return sessionFactory.getCurrentSession().get(Employee.class, emp.getEmpId());
	}

	/* (non-Javadoc)
	 * @see com.ssyt.ssh.dao.EmpDAO#updateEmp(com.ssyt.ssh.entity.Employee)
	 */
	@Override
	public void updateEmp(Employee emp) {
		sessionFactory.getCurrentSession().update(emp);

	}

	/* (non-Javadoc)
	 * @see com.ssyt.ssh.dao.EmpDAO#listEmp(com.ssyt.ssh.entity.Employee, int, int)
	 */
	@Override
	
	public List<Employee> listEmp(Employee emp, int pageSize, int currentPage) {
		StringBuffer sb = new StringBuffer("FROM Employee WHERE 1=1");
		List<Object> parameters = new ArrayList<Object>();
		// 判断是否存在分页条件
		if (emp != null) {
			if (emp.getEmpId() != null) {
				sb.append(" AND empId=?");
				parameters.add(emp.getEmpId());
			}
			
			if (emp.getEmpName()!= null && !"".equals(emp.getEmpName())) {
				sb.append(" AND empName like ?");
				parameters.add("%"+emp.getEmpName()+"%");
			}
			
			if (emp.getDept() != null && emp.getDept().getDeptId() != null) {
				sb.append(" AND dept.deptId=?");
				parameters.add(emp.getDept().getDeptId());
			}
		}
		
		// 创建Query对象
		Query<Employee> query = sessionFactory.getCurrentSession().createQuery(sb.toString());
		// 设置参数
		for (int i = 0; i < parameters.size(); i++) {
			query.setParameter(i, parameters.get(i));
		}
		// 设置分页参数
		query.setFirstResult((currentPage - 1) * pageSize);
		query.setMaxResults(pageSize);
		
		List<Employee> emps = query.list();
		
		return emps != null && emps.size() > 0 ? emps : null;
	}

	/* (non-Javadoc)
	 * @see com.ssyt.ssh.dao.EmpDAO#countEmp(com.ssyt.ssh.entity.Employee)
	 */
	@Override
	public int countEmp(Employee emp) {
		StringBuffer sb = new StringBuffer("SELECT COUNT(*) FROM Employee WHERE 1=1");
		List<Object> parameters = new ArrayList<Object>();
		// 判断是否存在分页条件
		if (emp != null) {
			if (emp.getEmpId() != null) {
				sb.append(" AND empId=?");
				parameters.add(emp.getEmpId());
			}
			
			if (emp.getEmpName()!= null && !"".equals(emp.getEmpName())) {
				sb.append(" AND empName like ?");
				parameters.add("%"+emp.getEmpName()+"%");
			}
			
			if (emp.getDept() != null && emp.getDept().getDeptId() != null) {
				sb.append(" AND dept.deptId=?");
				parameters.add(emp.getDept().getDeptId());
			}
		}
		// 创建Query对象
		Query<Long> query = sessionFactory.getCurrentSession().createQuery(sb.toString());
		// 设置参数
		for (int i = 0; i < parameters.size(); i++) {
			query.setParameter(i, parameters.get(i));
		}
		
		return query.getSingleResult().intValue();
	}

	@Override
	public List<Department> getDepts() {
		return sessionFactory.getCurrentSession().createQuery("FROM Department").list();
	}

}
